package com.zxc.controller.marketing;

import com.zxc.entity.CstCustomer;
import com.zxc.entity.SalChance;
import com.zxc.entity.SysUser;
import com.zxc.service.CstCustomerService;
import com.zxc.service.SalChanceService;
import com.zxc.service.SysUserService;
import com.zxc.utils.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author: zxc of Russell
 * @Date: 2021/9/19 18:55
 * @Version 1.0
 *
 */
@Controller
@RequestMapping("/marketing")
public class MarketingController {

    @Autowired
    private CstCustomerService cstCustomerService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    SalChanceService salChanceService;

    @RequestMapping(value = "/getCustomerByCustNo/{custNo}")
    @ResponseBody
    public Map<String,Object> detail(@PathVariable(value = "custNo") Integer custNo){
        Map<String,Object> modelMap = new HashMap<>();
        System.out.println("进入指派页面，接收到的客户的Id为="+custNo);
        CstCustomer cstCustomer = cstCustomerService.selectCstCustomerById(custNo);
        System.out.println("查询到的客户的信息="+cstCustomer);
        if (cstCustomer != null){
            modelMap.put("customer",cstCustomer);
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }


    @RequestMapping("/{skipCount}/{pageSize}")
    @ResponseBody
    public Map<String,Object> getSalChanceListOrByCustName(@PathVariable Integer skipCount,
                                                          @PathVariable Integer pageSize){
        Map<String,Object> modelMap = new HashMap<>();
        //计算出略过的记录数
        int skipCounts = (skipCount-1)*pageSize;
        List<SalChance> salChanceList = salChanceService.getSalChanceListByCondition(skipCounts,pageSize);
        modelMap.put("dataList",salChanceList);
        int total = salChanceService.selectCountSalChanceForMarketingByChcDueId();
        modelMap.put("total",total);
        return modelMap;
    }



    @RequestMapping(value = "/update")
    @ResponseBody
    public Map<String,Object> updateSalChanceAndCreateBy(SalChance salChance,String commonCreateBy){
        Map<String,Object> modelMap = new HashMap<>();
        if (!"".equals(commonCreateBy) && commonCreateBy != null){
            List<SysUser> sysUserList = sysUserService.selectSysUserById(Integer.parseInt(commonCreateBy));
            SysUser sysUser = sysUserList.get(0);
            salChance.setChcCreateBy(sysUser.getUserName());
        }else {
            if (salChance.getChcDueTo() != null && !salChance.getChcDueTo().equals("")){
                List<SysUser> sysUserList = sysUserService.selectSysUserById(Integer.parseInt(salChance.getChcDueTo()));
                SysUser sysUser = sysUserList.get(0);
                salChance.setChcDueId(IDUtil.createId());
                salChance.setChcDueTo(sysUser.getUserName());
                //1、正常 2、开发中 3、已归档 4、开发成功 5、开发失败
                salChance.setChcStatus("2");
                salChance.setChcDueDate(new Date());//这里是指的是创建的时候就指派,但是还是需要在前端加入日历插件
                System.out.println("机会处理编号"+salChance.getChcDueId());
            }
        }
        System.out.println("更新销售机会！！！！！！！！"+salChance);

        int result = salChanceService.updateSalChanceByChcId(salChance);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }



    @RequestMapping(value = "/getSalChanceByChcId/{chcId}")
    @ResponseBody
    public Map<String,Object> getCustomerByCustNo(@PathVariable(value = "chcId") Integer chcId){
        Map<String,Object> modelMap = new HashMap<>();
        SalChance salChance = salChanceService.selectSalChanceBychcId(chcId);
        //1、正常 2、开发中 3、已归档 4、开发成功 5、开发失败
        if (salChance.getChcStatus().equals("1")){
            salChance.setChcStatus("正常");
        }
        modelMap.put("salChance",salChance);
        if (salChance == null){
            modelMap.put("success",false);
        }else {
            modelMap.put("success",true);
        }
        return modelMap;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Integer[] chcId){
        System.out.println("删除一个或者多个用户="+ chcId);
        Map<String,Object> modelMap = new HashMap<>();
        int result = salChanceService.delete(chcId);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }

    @RequestMapping("/save")
    @ResponseBody
    public Map<String,Object > save(SalChance salChance){
        Map<String,Object> modelMap = new HashMap<>();

        //根据传入的创建人的信息来查询得到创建人的姓名
/*
        List<SysUser> sysUserList = sysUserService.selectSysUserById(Integer.parseInt(salChance.getChcCreateBy()));
*/
        List<SysUser> sysUserList = sysUserService.selectSysUserById(Integer.valueOf(salChance.getChcCreateId()));
        SysUser sysUser =  sysUserList.get(0);
        //给客户赋值默认值
        salChance.setChcCreateBy(sysUser.getUserName());
        salChance.setChcCreateId(IDUtil.createId());
        salChance.setChcCreateDate(new Date());
        salChance.setChcStatus("1");
        //这个参数应该是指派的时候赋值
        //salChance.setChcDueDate(new Date());
        if (salChance.getChcDueTo() != null && !salChance.getChcDueTo().equals("")){
            List<SysUser> sysUserList1 = sysUserService.selectSysUserById(Integer.parseInt(salChance.getChcDueTo()));
            SysUser sysUser1 = sysUserList1.get(0);
            salChance.setChcDueTo(sysUser1.getUserName());
            salChance.setChcDueId(IDUtil.createId());
            //1、正常 2、开发中 3、已归档 4、开发成功 5、开发失败
            salChance.setChcStatus("2");
            salChance.setChcDueDate(new Date());//这里是指的是创建的时候就指派,但是还是需要在前端加入日历插件
            System.out.println("机会处理编号"+salChance.getChcDueId());
        }

        int result = salChanceService.saveSalChance(salChance);
        if (result > 0){
            modelMap.put("success",true);
        }else {
            modelMap.put("success",false);
        }
        return modelMap;
    }
}
